package edu.ustb.aur.chain.interactive;

import edu.ustb.shellchainapi.bean.ChainParam;
import edu.ustb.shellchainapi.shellchain.command.ShellChainException;
import edu.ustb.utils.PropertiesUtil;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author
 * 主体链和客体链的创建，相关的调用方法
 */
@Service
public class ChainInteractiveService {

    /**
     * 链的实体类，稍后在实际应用中根据带参的构造函数实例化为资产链等具体对象
     */
    public ChainInteractiveDao chainInteractiveDao;

    /**
     * 不清楚作用
     */
    boolean isMock;

    /**
     * 默认构造函数
     */
    public ChainInteractiveService() {
        try {
            // 根据字符串获取区块链的参数，默认是资产链
            ChainParam chainParam = loadShellchainConfig("OBJ");
            // 创建区块链对象
            this.chainInteractiveDao = new ChainInteractiveDao(chainParam);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 带参构造函数
     */
    public ChainInteractiveService(String chainParam) {
        try {
            // 根据字符串获取区块链的参数
            ChainParam chainParam1 = loadShellchainConfig(chainParam);
            // 创建区块链对象
            this.chainInteractiveDao = new ChainInteractiveDao(chainParam1);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取配置文件的区块链信息
     * @param chain 区块链配置文件的信息的头（OBJ 或 SUB）
     * @return 返回ChainParam对象
     */
    ChainParam loadShellchainConfig(String chain) {
        // 读取配置文件
        Properties shellchainProperties = PropertiesUtil.getProperties("chain.properties");
        //
        String enableMock = shellchainProperties.getProperty("ENABLE_MOCK");
        //
        if (enableMock!=null || Boolean.valueOf(enableMock).equals(Boolean.FALSE)) {
            isMock = false;
            String chainName = shellchainProperties.getProperty(chain + "_SERVER_CHAINNAME");
            String serverIp = shellchainProperties.getProperty(chain + "_SERVER_IP");
            String serverPort = shellchainProperties.getProperty(chain + "_SERVER_PORT");
            String loginUser = shellchainProperties.getProperty(chain + "_SERVER_LOGIN");
            String loginPass = shellchainProperties.getProperty(chain + "_SERVER_PWD");
            return new ChainParam(1, chainName, serverIp, serverPort, loginUser, loginPass);
        }
        isMock = true;
        return null;
    }

    /**
     * 得到链的信息
     *
     * @return 链的信息
     */
    public String getChainInfo() throws ShellChainException {
        return chainInteractiveDao.getInfo();
    }

    /**
     * 把存的信息放在链上，返回交易id
     *
     * @param toAddress 链的地址，在application配置文件中配置
     * @param amount 0
     * @param attributes 存储的信息
     * @return 交易id
     */
    public String sendToChain(String toAddress, float amount, HashMap<String, Object> attributes) throws ShellChainException {
        return chainInteractiveDao.sendCustom(toAddress, amount, attributes);
    }

    /**
     * 通过交易id获取交易信息
     *
     * @param txId 交易id
     * @return 生成交易时存储的信息
     */
    public String getFromChain(String txId) throws ShellChainException{
        return chainInteractiveDao.getRaw(txId);
    }
}